× Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Lesson 6 Lesson 7 Lesson 8 Lesson 9 Lesson 10 Lesson 11 Lesson 12 Lesson 13 Lesson 14 Lesson 15 Lesson 16

All lessons

Lesson 16 - Input()

The input method can be very useful when a program you create needs information from the user. For instance you may need their name and age, or if it is a game, their username. Here's is the syntax for asking for someone's name:

name = input('Please enter your name: ')
print(name)

Let's say that I inputted into the console 'John Smith', without the quotation marks, the code would output 'John Smith', with quotation marks.

If we wanted to be a little more specific about their name, such as that we want to split their name up into first and last name, we could do this:

first_name, last_name = input().split()

We utilize the .split() function that we learned about early in the course, which was utilized to split up the string 'John Smith' so that first_name variable would be equal to 'John' and the last_name variable would be equal to 'Smith'.